﻿Imports System.Data
Imports System.Data.SqlClient

Partial Class Compte_EditerInfos
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.PanelModifier.Visible = True

        If Not Page.IsPostBack Then

            Me.LabelErreur.Text = ""

            Dim Utilisateur As MembershipUser = Membership.GetUser(HttpContext.Current.User.Identity.Name)
            If Not IsNothing(Utilisateur) Then
                Me.LabelLogin.Text = Utilisateur.UserName
                Me.TextBoxModifEmail.Text = Utilisateur.Email

                ' password
                Try
                    Me.TextBoxModifPasse.Text = Utilisateur.GetPassword("Makoa")
                Catch ex As Provider.ProviderException
                    ' password is hashed
                    Me.TextBoxModifPasse.Text = "*****"
                    Me.TextBoxModifPasse.ReadOnly = True
                    Me.TextBoxModifPasse.Enabled = False
                End Try

                ' profile
                Try
                    Dim pfc As ProfileCommon = ProfileCommon.Create(Utilisateur.UserName)
                    Me.TextBoxModifNom.Text = pfc.Nom
                Catch ex As Exception
                    Me.LabelErreur.Text &= ex.Message
                End Try

            Else
                Me.LabelErreur.Text &= "Impossible de retrouver l'utilisateur."
            End If

        End If ' If Not Page.IsPostBack Then

    End Sub

    Protected Sub ModifierUtilisateur(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonModifierUtilisateur.Click

        Dim nom As String = HttpContext.Current.User.Identity.Name
        Dim Utilisateur As MembershipUser = Membership.GetUser(nom)

        ' password
        Try
            Utilisateur.ChangePassword(Utilisateur.GetPassword, Me.TextBoxModifPasse.Text)
        Catch ex As Provider.ProviderException
            ' the password is hashed
        End Try
        Utilisateur.Email = Me.TextBoxModifEmail.Text
        Membership.UpdateUser(Utilisateur)

        ' profile
        Dim pfc As ProfileCommon = ProfileCommon.Create(nom)
        pfc.Nom = Me.TextBoxModifNom.Text
        pfc.Save()

        Response.Redirect("/Default.aspx")

    End Sub


End Class
